<?php
/**
 * +---------------------------------------------------------------------------+
 * | LL                             LLLL   LL     L   LLLL               LLLL  |
 * | LL                            LL   L  LLL   LL  LL   L             LL  LL |
 * | LL      LLLL   LLLLL  LL  LL  LL      LLLL LLL  LL         LL  LL      LL |
 * | LL         LL     LL  LL  LL  LL      L LLL LL  LLLLL      LL  LL     LL  |
 * | LL      LLLLL    LL    LLLL   LL      L  L  LL   LLLLL     LL  LL    LL   |
 * | LL     LL  LL   LL     LLLL   LL      L     LL      LL      LLLL    LL    |
 * | LL     LL  LL  LL       LL    LL   L  L     LL  L   LL      LLLL   LL     |
 * | LLLLLL  LLLLL  LLLLL    LL     LLLL   L     LL   LLLL        LL    LLLLLL |
 * |                        LL                                                 |
 * |                        LL                                                 |
 * +---------------------------------------------------------------------------+
 * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved.                  |
 * +---------------------------------------------------------------------------+
 * | LazyCMS is free software. See LICENSE for copyright notices and details.  |
 * +---------------------------------------------------------------------------+
 */
defined('COM_PATH') or die('Restricted access!');
/**
 * 时区设置
 *
 * @author  Lukin <my@lukin.cn>
 * @version $Id: timezone.php 743 2011-03-01 10:38:00Z mylukin $
 */
class Timezone {
    /**
     * 设置时区
     *
     * @param string $timezone
     * @return bool
     */
    function set_zone($timezone) {
        $zones = $this->get_list();
        if ($timezone && isset($zones[$timezone])) {
            if (function_exists('date_default_timezone_set')) {
                $result = date_default_timezone_get();
                date_default_timezone_set($timezone);
            } else {
                $result = getenv('TZ');
                putenv('TZ=' . $timezone);
            }
        } else {
            if (function_exists('date_default_timezone_set')) {
                $result = date_default_timezone_get();
            } else {
                $result = getenv('TZ');
            }
        }
        return $result;
    }
    /**
     * 时区列表
     *
     * @return array
     */
    function get_list() {
        $result = array();
        foreach ($this->get_group() as $name=>$zones) {
            foreach ($zones as $k=>$v) {
                if ($name == $k) {
                    $result[$name] = $v;
                } else {
                    $result[$name.'/'.$k] = $v;
                }
            }
        }
        return $result;
    }
    /**
     * 时区分组
     *
     * @return array
     */
    function get_group() {
        return array(
            'Africa' => array(
                'Abidjan' => 'Abidjan',
                'Accra' => 'Accra',
                'Addis_Ababa' => 'Addis Ababa',
                'Algiers' => 'Algiers',
                'Asmara' => 'Asmara',
                'Bamako' => 'Bamako',
                'Bangui' => 'Bangui',
                'Banjul' => 'Banjul',
                'Bissau' => 'Bissau',
                'Blantyre' => 'Blantyre',
                'Brazzaville' => 'Brazzaville',
                'Bujumbura' => 'Bujumbura',
                'Cairo' => 'Cairo',
                'Casablanca' => 'Casablanca',
                'Ceuta' => 'Ceuta',
                'Conakry' => 'Conakry',
                'Dakar' => 'Dakar',
                'Dar_es_Salaam' => 'Dar es Salaam',
                'Djibouti' => 'Djibouti',
                'Douala' => 'Douala',
                'El_Aaiun' => 'El Aaiun',
                'Freetown' => 'Freetown',
                'Gaborone' => 'Gaborone',
                'Harare' => 'Harare',
                'Johannesburg' => 'Johannesburg',
                'Kampala' => 'Kampala',
                'Khartoum' => 'Khartoum',
                'Kigali' => 'Kigali',
                'Kinshasa' => 'Kinshasa',
                'Lagos' => 'Lagos',
                'Libreville' => 'Libreville',
                'Lome' => 'Lome',
                'Luanda' => 'Luanda',
                'Lubumbashi' => 'Lubumbashi',
                'Lusaka' => 'Lusaka',
                'Malabo' => 'Malabo',
                'Maputo' => 'Maputo',
                'Maseru' => 'Maseru',
                'Mbabane' => 'Mbabane',
                'Mogadishu' => 'Mogadishu',
                'Monrovia' => 'Monrovia',
                'Nairobi' => 'Nairobi',
                'Ndjamena' => 'Ndjamena',
                'Niamey' => 'Niamey',
                'Nouakchott' => 'Nouakchott',
                'Ouagadougou' => 'Ouagadougou',
                'Porto-Novo' => 'Porto-Novo',
                'Sao_Tome' => 'Sao Tome',
                'Tripoli' => 'Tripoli',
                'Tunis' => 'Tunis',
                'Windhoek' => 'Windhoek',
            ),
            'America' => array(
                'Adak' => 'Adak',
                'Anchorage' => 'Anchorage',
                'Anguilla' => 'Anguilla',
                'Antigua' => 'Antigua',
                'Araguaina' => 'Araguaina',
                'Argentina' => 'Argentina',
                'Aruba' => 'Aruba',
                'Asuncion' => 'Asuncion',
                'Atikokan' => 'Atikokan',
                'Bahia' => 'Bahia',
                'Bahia_Banderas' => 'Bahia Banderas',
                'Barbados' => 'Barbados',
                'Belem' => 'Belem',
                'Belize' => 'Belize',
                'Blanc-Sablon' => 'Blanc-Sablon',
                'Boa_Vista' => 'Boa Vista',
                'Bogota' => 'Bogota',
                'Boise' => 'Boise',
                'Cambridge_Bay' => 'Cambridge Bay',
                'Campo_Grande' => 'Campo Grande',
                'Cancun' => 'Cancun',
                'Caracas' => 'Caracas',
                'Cayenne' => 'Cayenne',
                'Cayman' => 'Cayman',
                'Chicago' => 'Chicago',
                'Chihuahua' => 'Chihuahua',
                'Costa_Rica' => 'Costa Rica',
                'Cuiaba' => 'Cuiaba',
                'Curacao' => 'Curacao',
                'Danmarkshavn' => 'Danmarkshavn',
                'Dawson' => 'Dawson',
                'Dawson_Creek' => 'Dawson Creek',
                'Denver' => 'Denver',
                'Detroit' => 'Detroit',
                'Dominica' => 'Dominica',
                'Edmonton' => 'Edmonton',
                'Eirunepe' => 'Eirunepe',
                'El_Salvador' => 'El Salvador',
                'Fortaleza' => 'Fortaleza',
                'Glace_Bay' => 'Glace Bay',
                'Godthab' => 'Godthab',
                'Goose_Bay' => 'Goose Bay',
                'Grand_Turk' => 'Grand Turk',
                'Grenada' => 'Grenada',
                'Guadeloupe' => 'Guadeloupe',
                'Guatemala' => 'Guatemala',
                'Guayaquil' => 'Guayaquil',
                'Guyana' => 'Guyana',
                'Halifax' => 'Halifax',
                'Havana' => 'Havana',
                'Hermosillo' => 'Hermosillo',
                'Indiana' => 'Indiana',
                'Inuvik' => 'Inuvik',
                'Iqaluit' => 'Iqaluit',
                'Jamaica' => 'Jamaica',
                'Juneau' => 'Juneau',
                'Kentucky' => 'Kentucky',
                'La_Paz' => 'La Paz',
                'Lima' => 'Lima',
                'Los_Angeles' => 'Los Angeles',
                'Maceio' => 'Maceio',
                'Managua' => 'Managua',
                'Manaus' => 'Manaus',
                'Marigot' => 'Marigot',
                'Martinique' => 'Martinique',
                'Matamoros' => 'Matamoros',
                'Mazatlan' => 'Mazatlan',
                'Menominee' => 'Menominee',
                'Merida' => 'Merida',
                'Mexico_City' => 'Mexico City',
                'Miquelon' => 'Miquelon',
                'Moncton' => 'Moncton',
                'Monterrey' => 'Monterrey',
                'Montevideo' => 'Montevideo',
                'Montreal' => 'Montreal',
                'Montserrat' => 'Montserrat',
                'Nassau' => 'Nassau',
                'New_York' => 'New York',
                'Nipigon' => 'Nipigon',
                'Nome' => 'Nome',
                'Noronha' => 'Noronha',
                'North_Dakota' => 'North Dakota',
                'Ojinaga' => 'Ojinaga',
                'Panama' => 'Panama',
                'Pangnirtung' => 'Pangnirtung',
                'Paramaribo' => 'Paramaribo',
                'Phoenix' => 'Phoenix',
                'Port-au-Prince' => 'Port-au-Prince',
                'Port_of_Spain' => 'Port of Spain',
                'Porto_Velho' => 'Porto Velho',
                'Puerto_Rico' => 'Puerto Rico',
                'Rainy_River' => 'Rainy River',
                'Rankin_Inlet' => 'Rankin Inlet',
                'Recife' => 'Recife',
                'Regina' => 'Regina',
                'Resolute' => 'Resolute',
                'Rio_Branco' => 'Rio Branco',
                'Santa_Isabel' => 'Santa Isabel',
                'Santarem' => 'Santarem',
                'Santiago' => 'Santiago',
                'Santo_Domingo' => 'Santo Domingo',
                'Sao_Paulo' => 'Sao Paulo',
                'Scoresbysund' => 'Scoresbysund',
                'Shiprock' => 'Shiprock',
                'St_Barthelemy' => 'St Barthelemy',
                'St_Johns' => 'St Johns',
                'St_Kitts' => 'St Kitts',
                'St_Lucia' => 'St Lucia',
                'St_Thomas' => 'St Thomas',
                'St_Vincent' => 'St Vincent',
                'Swift_Current' => 'Swift Current',
                'Tegucigalpa' => 'Tegucigalpa',
                'Thule' => 'Thule',
                'Thunder_Bay' => 'Thunder Bay',
                'Tijuana' => 'Tijuana',
                'Toronto' => 'Toronto',
                'Tortola' => 'Tortola',
                'Vancouver' => 'Vancouver',
                'Whitehorse' => 'Whitehorse',
                'Winnipeg' => 'Winnipeg',
                'Yakutat' => 'Yakutat',
                'Yellowknife' => 'Yellowknife',
            ),
            'Antarctica' => array(
                'Casey' => 'Casey',
                'Davis' => 'Davis',
                'DumontDUrville' => 'DumontDUrville',
                'Macquarie' => 'Macquarie',
                'Mawson' => 'Mawson',
                'McMurdo' => 'McMurdo',
                'Palmer' => 'Palmer',
                'Rothera' => 'Rothera',
                'South_Pole' => 'South Pole',
                'Syowa' => 'Syowa',
                'Vostok' => 'Vostok',
            ),
            'Arctic' => array(
                'Longyearbyen' => 'Longyearbyen',
            ),
            'Asia' => array(
                'Aden' => 'Aden',
                'Almaty' => 'Almaty',
                'Amman' => 'Amman',
                'Anadyr' => 'Anadyr',
                'Aqtau' => 'Aqtau',
                'Aqtobe' => 'Aqtobe',
                'Ashgabat' => 'Ashgabat',
                'Baghdad' => 'Baghdad',
                'Bahrain' => 'Bahrain',
                'Baku' => 'Baku',
                'Bangkok' => 'Bangkok',
                'Beijing' => 'Beijing',
                'Beirut' => 'Beirut',
                'Bishkek' => 'Bishkek',
                'Brunei' => 'Brunei',
                'Choibalsan' => 'Choibalsan',
                'Chongqing' => 'Chongqing',
                'Colombo' => 'Colombo',
                'Damascus' => 'Damascus',
                'Dhaka' => 'Dhaka',
                'Dili' => 'Dili',
                'Dubai' => 'Dubai',
                'Dushanbe' => 'Dushanbe',
                'Gaza' => 'Gaza',
                'Harbin' => 'Harbin',
                'Ho_Chi_Minh' => 'Ho Chi Minh',
                'Hong_Kong' => 'Hong Kong',
                'Hovd' => 'Hovd',
                'Irkutsk' => 'Irkutsk',
                'Jakarta' => 'Jakarta',
                'Jayapura' => 'Jayapura',
                'Jerusalem' => 'Jerusalem',
                'Kabul' => 'Kabul',
                'Kamchatka' => 'Kamchatka',
                'Karachi' => 'Karachi',
                'Kashgar' => 'Kashgar',
                'Kathmandu' => 'Kathmandu',
                'Kolkata' => 'Kolkata',
                'Krasnoyarsk' => 'Krasnoyarsk',
                'Kuala_Lumpur' => 'Kuala Lumpur',
                'Kuching' => 'Kuching',
                'Kuwait' => 'Kuwait',
                'Macau' => 'Macau',
                'Magadan' => 'Magadan',
                'Makassar' => 'Makassar',
                'Manila' => 'Manila',
                'Muscat' => 'Muscat',
                'Nicosia' => 'Nicosia',
                'Novokuznetsk' => 'Novokuznetsk',
                'Novosibirsk' => 'Novosibirsk',
                'Omsk' => 'Omsk',
                'Oral' => 'Oral',
                'Phnom_Penh' => 'Phnom Penh',
                'Pontianak' => 'Pontianak',
                'Pyongyang' => 'Pyongyang',
                'Qatar' => 'Qatar',
                'Qyzylorda' => 'Qyzylorda',
                'Rangoon' => 'Rangoon',
                'Riyadh' => 'Riyadh',
                'Sakhalin' => 'Sakhalin',
                'Samarkand' => 'Samarkand',
                'Seoul' => 'Seoul',
                'Shanghai' => 'Shanghai',
                'Singapore' => 'Singapore',
                'Taipei' => 'Taipei',
                'Tashkent' => 'Tashkent',
                'Tbilisi' => 'Tbilisi',
                'Tehran' => 'Tehran',
                'Thimphu' => 'Thimphu',
                'Tokyo' => 'Tokyo',
                'Ulaanbaatar' => 'Ulaanbaatar',
                'Urumqi' => 'Urumqi',
                'Vientiane' => 'Vientiane',
                'Vladivostok' => 'Vladivostok',
                'Yakutsk' => 'Yakutsk',
                'Yekaterinburg' => 'Yekaterinburg',
                'Yerevan' => 'Yerevan',
            ),
            'Atlantic' => array(
                'Azores' => 'Azores',
                'Bermuda' => 'Bermuda',
                'Canary' => 'Canary',
                'Cape_Verde' => 'Cape Verde',
                'Faroe' => 'Faroe',
                'Madeira' => 'Madeira',
                'Reykjavik' => 'Reykjavik',
                'South_Georgia' => 'South Georgia',
                'St_Helena' => 'St Helena',
                'Stanley' => 'Stanley',
            ),
            'Australia' => array(
                'Adelaide' => 'Adelaide',
                'Brisbane' => 'Brisbane',
                'Broken_Hill' => 'Broken Hill',
                'Currie' => 'Currie',
                'Darwin' => 'Darwin',
                'Eucla' => 'Eucla',
                'Hobart' => 'Hobart',
                'Lindeman' => 'Lindeman',
                'Lord_Howe' => 'Lord Howe',
                'Melbourne' => 'Melbourne',
                'Perth' => 'Perth',
                'Sydney' => 'Sydney',
            ),
            'Europe' => array(
                'Amsterdam' => 'Amsterdam',
                'Andorra' => 'Andorra',
                'Athens' => 'Athens',
                'Belgrade' => 'Belgrade',
                'Berlin' => 'Berlin',
                'Bratislava' => 'Bratislava',
                'Brussels' => 'Brussels',
                'Bucharest' => 'Bucharest',
                'Budapest' => 'Budapest',
                'Chisinau' => 'Chisinau',
                'Copenhagen' => 'Copenhagen',
                'Dublin' => 'Dublin',
                'Gibraltar' => 'Gibraltar',
                'Guernsey' => 'Guernsey',
                'Helsinki' => 'Helsinki',
                'Isle_of_Man' => 'Isle of Man',
                'Istanbul' => 'Istanbul',
                'Jersey' => 'Jersey',
                'Kaliningrad' => 'Kaliningrad',
                'Kiev' => 'Kiev',
                'Lisbon' => 'Lisbon',
                'Ljubljana' => 'Ljubljana',
                'London' => 'London',
                'Luxembourg' => 'Luxembourg',
                'Madrid' => 'Madrid',
                'Malta' => 'Malta',
                'Mariehamn' => 'Mariehamn',
                'Minsk' => 'Minsk',
                'Monaco' => 'Monaco',
                'Moscow' => 'Moscow',
                'Oslo' => 'Oslo',
                'Paris' => 'Paris',
                'Podgorica' => 'Podgorica',
                'Prague' => 'Prague',
                'Riga' => 'Riga',
                'Rome' => 'Rome',
                'Samara' => 'Samara',
                'San_Marino' => 'San Marino',
                'Sarajevo' => 'Sarajevo',
                'Simferopol' => 'Simferopol',
                'Skopje' => 'Skopje',
                'Sofia' => 'Sofia',
                'Stockholm' => 'Stockholm',
                'Tallinn' => 'Tallinn',
                'Tirane' => 'Tirane',
                'Uzhgorod' => 'Uzhgorod',
                'Vaduz' => 'Vaduz',
                'Vatican' => 'Vatican',
                'Vienna' => 'Vienna',
                'Vilnius' => 'Vilnius',
                'Volgograd' => 'Volgograd',
                'Warsaw' => 'Warsaw',
                'Zagreb' => 'Zagreb',
                'Zaporozhye' => 'Zaporozhye',
                'Zurich' => 'Zurich',
            ),
            'Indian' => array(
                'Antananarivo' => 'Antananarivo',
                'Chagos' => 'Chagos',
                'Christmas' => 'Christmas',
                'Cocos' => 'Cocos',
                'Comoro' => 'Comoro',
                'Kerguelen' => 'Kerguelen',
                'Mahe' => 'Mahe',
                'Maldives' => 'Maldives',
                'Mauritius' => 'Mauritius',
                'Mayotte' => 'Mayotte',
                'Reunion' => 'Reunion',
            ),
            'Pacific' => array(
                'Apia' => 'Apia',
                'Auckland' => 'Auckland',
                'Chatham' => 'Chatham',
                'Chuuk' => 'Chuuk',
                'Easter' => 'Easter',
                'Efate' => 'Efate',
                'Enderbury' => 'Enderbury',
                'Fakaofo' => 'Fakaofo',
                'Fiji' => 'Fiji',
                'Funafuti' => 'Funafuti',
                'Galapagos' => 'Galapagos',
                'Gambier' => 'Gambier',
                'Guadalcanal' => 'Guadalcanal',
                'Guam' => 'Guam',
                'Honolulu' => 'Honolulu',
                'Johnston' => 'Johnston',
                'Kiritimati' => 'Kiritimati',
                'Kosrae' => 'Kosrae',
                'Kwajalein' => 'Kwajalein',
                'Majuro' => 'Majuro',
                'Marquesas' => 'Marquesas',
                'Midway' => 'Midway',
                'Nauru' => 'Nauru',
                'Niue' => 'Niue',
                'Norfolk' => 'Norfolk',
                'Noumea' => 'Noumea',
                'Pago_Pago' => 'Pago Pago',
                'Palau' => 'Palau',
                'Pitcairn' => 'Pitcairn',
                'Pohnpei' => 'Pohnpei',
                'Port_Moresby' => 'Port Moresby',
                'Rarotonga' => 'Rarotonga',
                'Saipan' => 'Saipan',
                'Tahiti' => 'Tahiti',
                'Tarawa' => 'Tarawa',
                'Tongatapu' => 'Tongatapu',
                'Wake' => 'Wake',
                'Wallis' => 'Wallis',
            ),
            'UTC' => array(
                'UTC' => 'UTC',
                'UTC-12' => 'UTC-12',
                'UTC-11.5' => 'UTC-11:30',
                'UTC-11' => 'UTC-11',
                'UTC-10.5' => 'UTC-10:30',
                'UTC-10' => 'UTC-10',
                'UTC-9.5' => 'UTC-9:30',
                'UTC-9' => 'UTC-9',
                'UTC-8.5' => 'UTC-8:30',
                'UTC-8' => 'UTC-8',
                'UTC-7.5' => 'UTC-7:30',
                'UTC-7' => 'UTC-7',
                'UTC-6.5' => 'UTC-6:30',
                'UTC-6' => 'UTC-6',
                'UTC-5.5' => 'UTC-5:30',
                'UTC-5' => 'UTC-5',
                'UTC-4.5' => 'UTC-4:30',
                'UTC-4' => 'UTC-4',
                'UTC-3.5' => 'UTC-3:30',
                'UTC-3' => 'UTC-3',
                'UTC-2.5' => 'UTC-2:30',
                'UTC-2' => 'UTC-2',
                'UTC-1.5' => 'UTC-1:30',
                'UTC-1' => 'UTC-1',
                'UTC-0.5' => 'UTC-0:30',
                'UTC+0' => 'UTC+0',
                'UTC+0.5' => 'UTC+0:30',
                'UTC+1' => 'UTC+1',
                'UTC+1.5' => 'UTC+1:30',
                'UTC+2' => 'UTC+2',
                'UTC+2.5' => 'UTC+2:30',
                'UTC+3' => 'UTC+3',
                'UTC+3.5' => 'UTC+3:30',
                'UTC+4' => 'UTC+4',
                'UTC+4.5' => 'UTC+4:30',
                'UTC+5' => 'UTC+5',
                'UTC+5.5' => 'UTC+5:30',
                'UTC+5.75' => 'UTC+5:45',
                'UTC+6' => 'UTC+6',
                'UTC+6.5' => 'UTC+6:30',
                'UTC+7' => 'UTC+7',
                'UTC+7.5' => 'UTC+7:30',
                'UTC+8' => 'UTC+8',
                'UTC+8.5' => 'UTC+8:30',
                'UTC+8.75' => 'UTC+8:45',
                'UTC+9' => 'UTC+9',
                'UTC+9.5' => 'UTC+9:30',
                'UTC+10' => 'UTC+10',
                'UTC+10.5' => 'UTC+10:30',
                'UTC+11' => 'UTC+11',
                'UTC+11.5' => 'UTC+11:30',
                'UTC+12' => 'UTC+12',
                'UTC+12.75' => 'UTC+12:45',
                'UTC+13' => 'UTC+13',
                'UTC+13.75' => 'UTC+13:45',
                'UTC+14' => 'UTC+14',
            ),
        );
    }
}

/**
 * 取得实例
 *
 * @return Timezone
 */
function &_timezone_get_object() {
	static $timezone;
	if ( is_null($timezone) )
		$timezone = new Timezone();
	return $timezone;
}